// pages/second/second.js
const DB = wx.cloud.database().collection("User")
var app = getApp()
Page({


  /**
   * 页面的初始数据
   */

  data: {
    status: ["已参加"],
    statID: 0,
    terms: ["2019", "2020", "2021", "2022"],
    tID: 0,//学期编号
    academy: ["信息学院"],
    aID: 0,//学院编号
    Temp_Events: [],
    events: [],
  },
  ChangeEvents: function () {
    var i = []
    var j = []
    var k = []
    let db = wx.cloud.database()
    db.collection('SecondEvent').get().then(res => {
      j = res.data
      DB.where({
        account: app.globalData.currentAccount
      }).get().then(res1 => {
        i = res1.data[0].AttendEvenID
        for (let m = 0; m < j.length; m++) {
          for (let n = 0; n < i.length; n++) {
            if (j[m].evenID === i[n]) {
              j[m].attend = true
              break
            }
          }
        }
        var z = 0
        if (this.data.status[this.data.statID] == "已参加") {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === true && j[m].tID - this.data.tID === 0) {
              k[z] = j[m]
              z++
            }
          }
        }
        else {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === false && j[m].tID - this.data.tID === 0) {
              k[z] = j[m]
              z++
            }
          }
        }
        this.setData({
          events: k
        })
      })
    })
  },
  TermChange: function (e) {
    console.log("term_select", e.detail.value)
    this.setData(
      {
        tID: e.detail.value
      }
    )
    this.ChangeEvents()
  },
  AcadeChange: function (e) {
    console.log("aca_select", e.detail.value)
    this.setData(
      {
        aID: e.detail.value
      }
    )
    this.ChangeEvents()
  },
  onPostTap:function(){
    wx.showToast({
      title: '请到二课堂页面修改！',
      icon: 'none',
      duration: 1500
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // ChangeEvents: function () {
  //   var i = []
  //   var j = []
  //   var k = []
  //   let db = wx.cloud.database()
  //   db.collection('SecondEvent').get().then(res => {
  //     j = res.data
  //     DB.where({
  //       account: app.globalData.currentAccount
  //     }).get().then(res1 => {
  //       i = res1.data[0].AttendEvenID
  //       for (let m = 0; m < j.length; m++) {
  //         for (let n = 0; n < i.length; n++) {
  //           if (j[m].evenID === i[n]) {
  //             j[m].attend = true
  //             break
  //           }
  //         }
  //       }
  //       var z = 0
  //       if (this.data.status[this.data.statID] == "已参加") {
  //         for (let m = 0; m < j.length; m++) {
  //           if (j[m].attend === true && j[m].tID - this.data.tID === 0) {
  //             k[z] = j[m]
  //             z++
  //           }
  //         }
  //       }
  //       else {
  //         for (let m = 0; m < j.length; m++) {
  //           if (j[m].attend === false && j[m].tID - this.data.tID === 0) {
  //             k[z] = j[m]
  //             z++
  //           }
  //         }
  //       }
  //       this.setData({
  //         events: k
  //       })
  //     })
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var i = []
    var j = []
    var k = []
    let db = wx.cloud.database()
    db.collection('SecondEvent').get().then(res => {
      j = res.data
      DB.where({
        account: app.globalData.currentAccount
      }).get().then(res1 => {
        i = res1.data[0].AttendEvenID
        for (let m = 0; m < j.length; m++) {
          for (let n = 0; n < i.length; n++) {
            if (j[m].evenID === i[n]) {
              j[m].attend = true
              break
            }
          }
        }
        var z = 0
        if (this.data.status[this.data.statID] == "已参加") {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === true && j[m].tID === this.data.tID) {
              k[z] = j[m]
              z++
            }
          }
        }
        else {
          for (let m = 0; m < j.length; m++) {
            if (j[m].attend === false && j[m].tID === this.data.tID) {
              k[z] = j[m]
              z++
            }
          }
        }
        this.setData({
          events: k
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.ChangeEvents(),
      wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})